var address = getStatusData("/basicInfoController/selectOriginType.html").rows;
var treeData = getStatusData("/basicInfoController/goodsTree.html");

//过滤出不含撤销状态的行
function getUsefulRows(subRows){
    var usefulRows = [];
    var count = 0;
    $.each(subRows,function(index,value){
        if(value.statusSub != "撤销"){
            usefulRows.push(value);
            count++;
        }
    });
    return usefulRows;
}

//总金额等于各子项金额总计之和，总重量等于各子项重量之和，含税货款金额等于各子项货款金额之和，税额等于各子项税额之和。
function calculateToTop() {
    var subRows = getUsefulRows($('#subItem').datagrid('getRows'));
    var totalM = 0;
    var totalW = 0;
    var totalHaveTax = 0;
    var totalTax = 0;
    $.each(subRows, function (index, value) {
        totalM = totalM.add(parseFloat(value.totalCost ? value.totalCost : 0));
        totalW = totalW.add(parseFloat(value.weight ? value.weight : 0));
        totalHaveTax = totalHaveTax.add(parseFloat(value.paymentAmount ? value.paymentAmount : 0));
        totalTax = totalTax.add(parseFloat(value.subtaxAmount ? value.subtaxAmount : 0));
    });
    $('#totalAmount').numberbox('setValue',totalM.toFixed(2));
    $('#totalWeight').numberbox('setValue',totalW);
    $('#taxGoodsAmount').numberbox('setValue',totalHaveTax.toFixed(2));
    $('#taxAmount').numberbox('setValue',totalTax.toFixed(2));
}

//供应商代码 签约人不能为空
function validateTopSubs(flag) {
    if(flag == undefined){
        var userRow = $('#userName').combogrid('grid').datagrid('getSelected');
        var memberRow = $('#memberNo').combogrid('grid').datagrid('getSelected');
    }else{
        var userRow = $('#userName').val();
        var memberRow = $('#memberNo').val();
    }

    if (!(memberRow)) {
        return "供应商代码不能为空";
    } else if (!(userRow)) {
        return "签约人不能为空";
    }
    return "true";
}

function validateBottomSubs(data) {
    if (data.length > 0) {
        var str = "";
        $.each(data, function (index, value) {
            var hangNo = index + 1;
            if (!isMoreThan0Number(value.weight)||value.weight< 0) {
                str += "第 " + hangNo + " 行" + "重量为非空正数" + "<br/>";
            }
            if (!isMoreThan0Number(value.unitPrice)||value.unitPrice<0) {
                str += "第 " + hangNo + " 行" + "单价为非空正数" + "<br/>";
            }
            if (isEmpty(value.f_pm_id)) {
                str += "第 " + hangNo + " 行" + "品名为非空" + "<br/>";
            }
            if (isEmpty(value.marks)) {
                str += "第 " + hangNo + " 行" + "牌号为非空" + "<br/>";
            }
            if (isEmpty(value.cpspecification)) {
                str += "第 " + hangNo + " 行" + "规格为非空" + "<br/>";
            }
            if (isEmpty(value.f_cd_id)) {
                str += "第 " + hangNo + " 行" + "产地为非空" + "<br/>";
            }
            if (isEmpty(value.deliveryMethod)) {
                str += "第 " + hangNo + " 行" + "交货方式为非空" + "<br/>";
            }
        });
        if (str === "") {
            str = "true";
        }
        return str;
    } else {
        return "必须包含有效子项";
    }
}

